package com.aracky.assess.service.impl;

import com.aracky.assess.mapper.HrKpiUserPerformanceDetailMapper;
import com.aracky.assess.model.entity.HrKpiPostPerformanceConfig;
import com.aracky.assess.model.entity.HrKpiUserAssessorConfig;
import com.aracky.assess.model.entity.HrKpiUserPerformanceDetail;
import com.aracky.assess.model.vo.HrKpiUserPerformanceDetailVO;
import com.aracky.assess.model.vo.SysDeptPostName;
import com.aracky.assess.service.HrKpiPostPerformanceConfigService;
import com.aracky.assess.service.HrKpiUserAssessorConfigService;
import com.aracky.assess.service.HrKpiUserPerformanceDetailService;
import com.aracky.common.util.ConvertUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 用户绩效考核明细 服务实现类
 * </p>
 *
 * @author zzp
 * @since 2022-08-01 09:48:11
 */
@Service
public class HrKpiUserPerformanceDetailServiceImpl extends ServiceImpl<HrKpiUserPerformanceDetailMapper, HrKpiUserPerformanceDetail> implements HrKpiUserPerformanceDetailService {


    @Resource
    private HrKpiPostPerformanceConfigService hrKpiPostPerformanceConfigService;

    @Resource
    private HrKpiUserPerformanceDetailService hrKpiUserPerformanceDetailService;

    @Resource
    private HrKpiUserPerformanceDetailMapper hrKpiUserPerformanceDetailMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveInfos(List<HrKpiUserAssessorConfig> hrKpiUserAssessorConfigList, List<Long> postIds) {
        LambdaQueryWrapper<HrKpiPostPerformanceConfig> lambdaQueryWrapper1 = new LambdaQueryWrapper<HrKpiPostPerformanceConfig>()
                .in(HrKpiPostPerformanceConfig::getPostId, postIds);
        //通过岗位id查询HrKpiPostPerformanceConfig表的信息
        List<HrKpiPostPerformanceConfig> list = hrKpiPostPerformanceConfigService.list(lambdaQueryWrapper1);
        //创建HrKpiUserPerformanceDetail对象，把HrKpiPostPerformanceConfig对象的值赋值给HrKpiUserPerformanceDetail对象
        HrKpiUserPerformanceDetail hrKpiUserPerformanceDetail;
        List<HrKpiUserPerformanceDetail> hrKpiUserPerformanceDetailList = new ArrayList<>();
        for (HrKpiPostPerformanceConfig hrKpiPostPerformanceConfig : list) {
            hrKpiPostPerformanceConfig.setId(null);
            for (HrKpiUserAssessorConfig hrKpiUserAssessorConfig : hrKpiUserAssessorConfigList) {
                if (hrKpiUserAssessorConfig.getPostId().equals(hrKpiPostPerformanceConfig.getPostId())) {
                    hrKpiUserPerformanceDetail = ConvertUtil.sourceToTarget(hrKpiPostPerformanceConfig, HrKpiUserPerformanceDetail.class);
                    hrKpiUserPerformanceDetail.setHId(hrKpiUserAssessorConfig.getUserId());
                    hrKpiUserPerformanceDetailList.add(hrKpiUserPerformanceDetail);
                }
            }
        }

        // for (HrKpiUserAssessorConfig hrKpiUserAssessorConfig : hrKpiUserAssessorConfigList) {
        //     HrKpiUserPerformanceDetail hrKpiUserPerformanceDetail = new HrKpiUserPerformanceDetail();
        //     for (HrKpiPostPerformanceConfig hrKpiPostPerformanceConfig : list) {
        //         if (hrKpiUserAssessorConfig.getPostId().equals(hrKpiPostPerformanceConfig.getPostId())){
        //             hrKpiUserPerformanceDetail = ConvertUtil.sourceToTarget(hrKpiPostPerformanceConfig, HrKpiUserPerformanceDetail.class);
        //         }
        //     }
        // }

        hrKpiUserPerformanceDetailService.saveBatch(hrKpiUserPerformanceDetailList);
    }

    /**
     * 通过配置id获取Detail信息
     *
     * @param id
     * @return
     */
    @Override
    public HrKpiUserPerformanceDetailVO getDetailInfo(Long id) {
        HrKpiUserPerformanceDetail hrKpiUserPerformanceDetail = hrKpiUserPerformanceDetailService.getById(id);
        HrKpiUserPerformanceDetailVO hrKpiUserPerformanceDetailVO = ConvertUtil.sourceToTarget(hrKpiUserPerformanceDetail, HrKpiUserPerformanceDetailVO.class);
        return hrKpiUserPerformanceDetailVO;
    }

    @Override
    public SysDeptPostName getDeptPostNameInfo(Long postId) {
        return hrKpiUserPerformanceDetailMapper.getDeptPostNameInfo(postId);
    }
}
